package com.wwwarehouse.resource_center.fragment.createobject.modifydelete;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.wwwarehouse.common.bean.upload.FileUploadBean;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.bean.goods.ModifyDeleteBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.createobject.createtools.CreateToolsFragment;
import com.wwwarehouse.resource_center.utils.UploadPicUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Joseph on 2017/10/24.
 *         <p/>
 *          修改工具
 */

public class ModifyToolsFragment extends CreateToolsFragment {

    private ModifyDeleteBean.ListBean bean;
    private String mImgUrl;
    @Override
    public void setTitle() {
        if (peekFragment() instanceof ModifyStockFragment){
            mActivity.setTitle(getString(R.string.modify_tool));
        }
    }

    @Override
    protected void loadDatas() {
//        mToolsName.setBackgroundColor(mActivity.getResources().getColor(R.color.white));
        img_name.setVisibility(View.GONE);
        mToolsAttribute.setSelected(true);
        mToolsAttribute.setOnClickListener(this);
        mLLPicture.setVisibility(View.GONE);
        mPic.setVisibility(View.VISIBLE);
        mToolsPicture.setSelected(false);
//        mName.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mName.getLayoutParams();
        layoutParams.topMargin = 0;
        mName.setLayoutParams(layoutParams);
        Bundle bundle = getArguments();
        if (null!=bundle){
            pos = bundle.getInt("pos");
            bean = bundle.getParcelable("data");
            ownerUkid = bundle.getString("ownerUkid");
            abstractObjectUkid = bean.getAbstractObjectUkid();
            toolName =bean.getName();
            mName.setText(toolName);
            toolDes = bean.getIntroduction();
            mDes.setText(toolDes);
            if (!TextUtils.isEmpty(bean.getUnitCharacter() )){
                mUnitCharacter ="（"+ bean.getUnitCharacter()+"）";
            }
            mUnit.setText((bean.getUnitName() + mUnitCharacter));
            unitUkid = (bean.getUnitUkid());
            try {
                mTagMsg = bean.getIdentifyList().get(0).getIdentifyCode();
                mTag.setText(mTagMsg);
                mCategory.setText(bean.getCategoryList().get(0).getName());
                categoryUkid = (bean.getCategoryList().get(0).getCategoryUkid());
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                List<ModifyDeleteBean.ListBean.LabelListBean> labelList = bean.getLabelList();
                String jsonString = JSON.toJSONString(labelList);
                data = (ArrayList<GoodsSelectLabelBean.ListBean>) JSONObject.parseArray(jsonString, GoodsSelectLabelBean.ListBean.class);
                setLaber(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
            List<ModifyDeleteBean.ListBean.AttributeListBean> attributeList = bean.getAttributeList();
            listBeen = (ArrayList<GoodsAttributesBean.ListBean>) JSONObject.parseArray(JSON.toJSONString(attributeList), GoodsAttributesBean.ListBean.class);
            setAttributes(listBeen);
            try {
                mImgUrl = bean.getMediaList().get(0).getRelatedUrl();
                fileUploadBean = new FileUploadBean();
                List<FileUploadBean.DataBean> dataBeen = new ArrayList<>();
                FileUploadBean.DataBean dataBean = new FileUploadBean.DataBean();
                List<ModifyDeleteBean.ListBean.MediaListBean.SubMediasBean> subMedias = bean.getMediaList().get(0).getSubMedias();
                if (subMedias!=null&&subMedias.size()>0){
                    for (int i = 0; i < subMedias.size(); i++) {
                        if (subMedias.get(i).getSubMediaType().equals("MEDIUM_IMAGE")){
                            dataBean.setName(bean.getMediaList().get(0).getMediaName());
                            dataBean.setPath(subMedias.get(i).getSubMediaUrl());
                            dataBeen.add(dataBean);
                            fileUploadBean.setData(dataBeen);
                            break;
                        }
                    }
                }else {
                    dataBean.setName(bean.getMediaList().get(0).getMediaName());
                    dataBean.setPath(bean.getMediaList().get(0).getRelatedUrl());
                    dataBeen.add(dataBean);
                    fileUploadBean.setData(dataBeen);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (TextUtils.isEmpty(mImgUrl)){
                mLLPicture.setVisibility(View.VISIBLE);
                img_descrip.setText("暂无图片");
                no_pic.setBackgroundResource(R.drawable.picture_no);
                img_descrip.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
            }else {
                mLLPicture.setVisibility(View.GONE);
                mPic.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(mImgUrl, mPic, new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                        mLLPicture.setVisibility(View.VISIBLE);
                        img_descrip.setText("暂无图片");
//                                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) img_descrip.getLayoutParams();
//                                    layoutParams.topMargin = 100;
//                                    img_descrip.setLayoutParams(layoutParams);
                        no_pic.setBackgroundResource(R.drawable.picture_no);
                        img_descrip.setTextColor(mActivity.getResources().getColor(R.color.common_color_c3_ffffff));
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
            }
        }
    }
    @Override
    public void titleBt1Click() {
        if (peekFragment() instanceof ModifyToolsFragment){
            if (path != null && !path.isEmpty()) {
                showProgressDialog("图片上传中...");
                UploadPicUtil.getInstance().uploadPic(path, mFile, getActivity());
            } else {
                request(ResourceConstant.UPDATE_ABSTRACT_OBJECT,"UPDATE_H_TOOLS");
            }
        }
    }
}
